package otlTwitter;

import processing.core.*; // processing
import java.util.List;
import java.util.ArrayList;
import twitter4j.*; // twitter tools
import twitter4j.conf.ConfigurationBuilder; 

public class geoTwitterOtl extends PApplet {
	
    private static final long serialVersionUID = 1;
    
	public void setup() {
		size(1100,900);
		background(0);
		stroke(255);
	}
/*
    ConfigurationBuilder cb = new ConfigurationBuilder();

    public void configure() {
		println("testing");
    cb.setOAuthConsumerKey("1joTbvy2N5iowiJkvMPoQ");
    cb.setOAuthConsumerSecret("IjkHewjxLJB6x8pH1Y01MTKBw05btAN7WeXUITeoVY");
    cb.setOAuthAccessToken("5451722-tSdcgntgBCPrwi9TEmADWTvNw4VyFVbuSSoHnQPY4S");
    cb.setOAuthAccessTokenSecret("h3e9Ve3IwRAj2Gle8sTtir6ddyd5WddyKabuiWlrsPhlP");
    } // end configure
    
    Twitter twitter = new TwitterFactory(cb.build()).getInstance();
    Query galt = new Query("uwaterloo"); // this is what we'll search for, uses Query object
    
    int locationCount = 0;
    int nullCount = 0;
    
    public void geoParams(){
        GeoLocation galtlocale = new GeoLocation(43.395132, -80.324993);
        Double hundoRadius = 20.0; // radius, in kilometers as specified below
        galt.setCount(100); // search for all the tweets, max 100/page
        galt.setGeoCode(galtlocale, hundoRadius, Query.KILOMETERS); // (geoLocation, double radius, unit for radius);
        galt.setSince("2014-04-28");
    }
    
    ArrayList<String> msgs = new ArrayList<String>();
	ArrayList<Status> tweets = new ArrayList<Status>();
    
	public void geoSearchTweets() { // sets up twitterfactory and populate ArrayList<Status> tweets and ArrayList<String> msgs
	      ConfigurationBuilder cb = new ConfigurationBuilder();
	      cb.setOAuthConsumerKey("1joTbvy2N5iowiJkvMPoQ");
	      cb.setOAuthConsumerSecret("IjkHewjxLJB6x8pH1Y01MTKBw05btAN7WeXUITeoVY");
	      cb.setOAuthAccessToken("5451722-tSdcgntgBCPrwi9TEmADWTvNw4VyFVbuSSoHnQPY4S");
	      cb.setOAuthAccessTokenSecret("h3e9Ve3IwRAj2Gle8sTtir6ddyd5WddyKabuiWlrsPhlP");
	      
	      // this is a twitter object, it does the twitter things
	      Twitter twitter = new TwitterFactory(cb.build()).getInstance();
	      
	      GeoLocation galtlocale = new GeoLocation(43.395132, -80.324993);
	      Double hundoRadius = 1.0; // radius, in kilometers as specified below	      
	      Query galt = new Query(); // this is what we'll search for, uses Query object
	      galt.setCount(100); // search for all the tweets, max 100/page
	      galt.setGeoCode(galtlocale, hundoRadius, Query.KILOMETERS); // (geoLocation, double radius, unit for radius);
	      galt.setSince("2014-04-22");
	      
	      
	      // sometimes this will not work, try/catch loop handles the exception it throws. neat.
	      try {
	    	  QueryResult result = twitter.search(galt);
	    	  List<Status> pageTweets = result.getTweets();
	    	  tweets.addAll(pageTweets);
	    	  int i = 0;
	    	  while (result.hasNext() == true){
	    	  result = twitter.search(result.nextQuery()); // iterate to next page
	    	  println("pages: " + i); // and let me know
	    	  i += 1;
	    	  pageTweets = result.getTweets(); // tweets is your list of all tweets from QueryResult, 
	    	  tweets.addAll(pageTweets);
	    	  }
	    	  // now have ArrayList<Status> of all pages - or should
	      }
	      catch (TwitterException te) {
	    	  String errormsg = "Couldn't connect: " + te;
	    	  println(errormsg);
	      }
	      
	    	 for (int x=0; x<tweets.size(); x++) { // this loop writes the output for testing
	    		  Status tweet = tweets.get(x); 			// 'tweets.get' gets tweet by index : now you have the TweetEntity at index i
	    		  String tweettext = tweet.getText();		// now pull text
	    		  GeoLocation tweetlocate = tweet.getGeoLocation(); // pull location
	    		  try { 									// a lot of tweets don't have location data, use this to catch nulls
	    		  String tweetlatstr = Double.toString(tweetlocate.getLatitude());
	    		  String tweetlongstr = Double.toString(tweetlocate.getLongitude());
	    		  msgs.add(" | " + tweetlatstr + " | " + tweetlongstr + " | " + tweettext);
	    		  locationCount = locationCount + 1;
	    		  } catch (NullPointerException e) {
	    			  String tweetlatstr = "| no lat ";
	    			  String tweetlongstr = " no long | "; // most are null.... try pagination until you hit results?
	    			  msgs.add(tweetlatstr + " | " + tweetlongstr + tweettext);
	    			  nullCount = nullCount + 1;
	    		  } // end catch
	   }
}
*/
	public void draw() {
		textSize(11);
		text("Location Checks:", 60,40);
		/*
		text("# of: " + msgs.size() + " | locations: " + locationCount + " | nulls: " + nullCount, 40,80); // wherein configure() should return msg ...
			for (int i = 0; i < msgs.size(); i++) {
				text(msgs.get(i), 40, 100+15*(i+1));
			}
			*/
	}
	
	//include to enable run as java application instead of applet
	public static void main(String args[]) {
		PApplet.main(new String[] { "--present", "otlTwitter.geoTwitterOtl" });
	}

    }
